package com.dongdongshop.controller;

import cn.hutool.json.JSONUtil;
import com.dongdongshop.data.Result;
import com.dongdongshop.enums.ResultEnums;
import com.dongdongshop.pojo.TbTypeTemplate;
import com.dongdongshop.service.TbSpecificationOptionSerive;
import com.dongdongshop.service.TbTypeTemplateService;
import com.dongdongshop.vo.TbSpecificationOptionVO;
import com.dongdongshop.vo.TempletVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("TbSpecificationOption")
public class TbSpecificationOptionController {

    //注入service
    @DubboReference
    private TbSpecificationOptionSerive tbSpecificationOptionSerive;
    @DubboReference
    private TbTypeTemplateService tbTypeTemplateService;

    //查询规格全部数据(进行会先)
    @ResponseBody
    @RequestMapping("TbSpecificationshowbyid")
    public Result TbSpecificationshow(Long id) {
        try {
            //获取模板信息
            TbTypeTemplate tbTypeTemplate = tbTypeTemplateService.showbyid(id);
            String specIds = tbTypeTemplate.getSpecIds();
            //将模板类中specIds字段转成对象
            List<TbSpecificationOptionVO> tbSpecificationOptionVOS = JSONUtil.toList(specIds, TbSpecificationOptionVO.class);
            for (TbSpecificationOptionVO optionVO:tbSpecificationOptionVOS) {
                TbSpecificationOptionVO showecho = tbSpecificationOptionSerive.showecho(optionVO.getId());
                optionVO.setRows(showecho.getRows());
            }
            TempletVO templetVO = new TempletVO();
            BeanUtils.copyProperties(tbTypeTemplate,templetVO);
            templetVO.setTbSpecificationOptionVOSList(tbSpecificationOptionVOS);
            return Result.response(ResultEnums.ECHO_SUCCESS).setData(templetVO);
        } catch (Exception e) {
            return Result.response(ResultEnums.ECHO_ERROR);
        }
    }
}
